package com.company;

import com.company.iterator.Car;
import com.company.iterator.CarGarageAggregate;
import com.company.iterator.Iterator;

/**
 * 迭代器设计模式测试代码
 */
public class TestForIterator {

    public static void main(String[] args) {
        //初始化数据
        CarGarageAggregate carGarage = new CarGarageAggregate();
        Car car1 = new Car("宾利");
        Car car2 = new Car("保时捷");
        Car car3 = new Car("劳斯莱斯");
        Car car4 = new Car("玛莎拉蒂");
        carGarage.add(car1);
        carGarage.add(car2);
        carGarage.add(car3);
        carGarage.add(car4);

        System.out.println("遍历车库中的汽车：");
        //遍历车库中的汽车
        Iterator it = carGarage.iterator();
        while (it.hasNext()) {
            Car car = (Car) it.next();
            System.out.println(car.getName());
        }

        System.out.println("======================");

        //获取汽车总数
        System.out.println("汽车总数："+carGarage.length());

        System.out.println("======================");

        //根据下标获取汽车
        System.out.println("下标为1的汽车："+carGarage.get(1).getName());

        System.out.println("======================");

        //移除汽车后再遍历车库中的汽车
        System.out.println("移除汽车后再遍历车库中的汽车：");
        carGarage.remove(car1);
        Iterator it2 = carGarage.iterator();
        while (it2.hasNext()) {
            Car car = (Car) it2.next();
            System.out.println(car.getName());
        }

    }
}
